package com.du.javaweb.servlet; /**
 * @author : DuJiabao
 * @Date : 2021/3/28 16:01
 * @Project : java-web
 * @File : ${FILE_NAME}
 * @Desc :
 */

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "LoginServlet", value = "/LoginServlet")
public class LoginServlet extends HttpServlet {
  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.setCharacterEncoding("UTF-8");
    response.setContentType("text/html; charset=UTF-8");
    String name = request.getParameter("name");
    String password = request.getParameter("password");

    if ("jack".equals(name) && "123".equals(password)) {
      Cookie nameCookie = new Cookie("name", name);
      Cookie passwordCookie = new Cookie("password", password);
      response.addCookie(nameCookie);
      response.addCookie(passwordCookie);
      response.getWriter().write("登录成功");
      return;
    }

    RequestDispatcher dispatcher = request.getRequestDispatcher("/login.jsp");
    dispatcher.forward(request, response);
  }
}
